package com.xing.retry;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Author: linjx
 * Date: 2018/9/25
 */
class RpcThreadPoolConfig {

    private final static int MAX_THREADS = 10;

    private final ExecutorService EXECUTOR_SERVICE;

    private RpcThreadPoolConfig(){
        ThreadPoolExecutor poolExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool(new ThreadFactory() {
            private AtomicInteger max = new AtomicInteger(0);
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "rpc-retry-" + max.incrementAndGet());
            }
        });
        poolExecutor.setMaximumPoolSize(MAX_THREADS);
        EXECUTOR_SERVICE = poolExecutor;
    }

    static ExecutorService executor() {
        return RetryThreadPoolConfig.CONFIG.EXECUTOR_SERVICE;
    }

    private static class RetryThreadPoolConfig {
        private static final RpcThreadPoolConfig CONFIG = new RpcThreadPoolConfig();
    }

}
